package com.fgr.service.Impl;

import com.fgr.beans.User;
import com.fgr.mapper.UserMapper;
import com.fgr.service.UserService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public List<User> selectAllUser() {
        List<User> list = userMapper.selectAllUser();
        return list;
    }

    @Override
    public String selectUser(User user,HttpServletRequest request) {
        User user1 = userMapper.selectUser(user);

        if (user1!=null){
            HttpSession session = request.getSession();
            session.setAttribute("username",user1.getUsername());
            session.setAttribute("password",user1.getPassword());
            session.setAttribute("userid",user1.getuId());

            request.setAttribute("loginUser", user1);
            return "SUCCESS";
        }else {
            return "ERRO";
        }
    }



    @Override
    public String deleteUserById(Integer uid) {
        Integer integer = userMapper.deleteUserById(uid);
        if (integer>0){
            return "SUCCESS";
        }else {
            return "ERRO";
        }
    }

    @Override
    public String insertUser(User user) {
        User user1 = userMapper.selectUserName(user);
        if (user1==null){
            Integer integer = userMapper.insertUser(user);
            if (integer>0){
                return "SUCCESS";
            }else {
                return "ERRO";
            }
        }else {
            return "已存在";
        }

    }

    @Override
    public User findUserById(Integer id) {
        User user = userMapper.findUserById(id);
        return user;
    }

    @Override
    public String UpdateUser(User user) {
        Integer integer = userMapper.UpdateUser(user);
        if (integer>0){
            return "SUCCESS";
        }else {
            return "ERRO";
        }
    }
}
